import java.util.Date;

class Info {
	private String name ;
	private int content;
	private boolean flag = false;
	private int currentNum = 0;
	private int tempnum = 0;
	private final int maxRequest = 5;

	public synchronized void set(String name, int content) {
		if (!flag) {
			try {
				super.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		this.setName(name);
		currentNum += 1;
		tempnum += content;
		if (currentNum == this.maxRequest) {
			this.setContent(tempnum);
			tempnum = 0;
			currentNum = 0;
		}
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		flag = false;
		super.notify();
	}

	public synchronized void get() throws InterruptedException {
		if (flag) {
			try {
				super.wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		try {
			Thread.sleep(300);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		System.out.println(this.getName() + " --->  " + "[ "+ this.getContent() + " ]");
		tempnum += this.getContent();
		this.setCurrentNum(this.getCurrentNum() + 1);
		if (this.getCurrentNum() == maxRequest) {
			Date dt = new Date();
			System.out.println("\n****Submit Success ---=>   " + tempnum + "\n");
			System.out.println("\n****Submit Date    ---=>   " + dt.toString()
					+ "\n");
			flag = true;
		}

		if (flag == true) {
			this.setCurrentNum(0);
			super.notify();
		}
	}

	public void setName(String name) {
		this.name = name;
	}

	public void setContent(int content) {
		this.content = content;
	}

	public String getName() {
		return this.name;
	}

	public int getContent() {
		return this.content;
	}

	public int getCurrentNum() {
		return currentNum;
	}

	public void setCurrentNum(int currentNum) {
		this.currentNum = currentNum;
	}
};

class Producer implements Runnable {
	private Info info = null;

	public Producer(Info info) {
		this.info = info;
	}

	public void run() {
		boolean flag = false;
		for (int i = 1; i <= 10; i++) {
			if (flag) {
				this.info.set("User Request", (int) (Math.random() * 10));
				flag = true;
			} else {
				this.info.set("User Request", (int) (Math.random() * 10));
				flag = false;
			}
		}
	}
};

class Consumer implements Runnable {
	private Info info = null;

	public Consumer(Info info) {
		this.info = info;
	}

	public void run() {
		for (int i = 1; i <= 9; i++) {
			try {
				this.info.get();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.exit(1);
	}
};

public class DynaProxyDemo {
	public static void main(String args[]) throws Exception{
		Info info = new Info();
		Producer pro = new Producer(info);
		Consumer con = new Consumer(info);
		new Thread(pro).start();
		new Thread(con).start();
		Thread.sleep((int)(Math.random()*10*500));
	}
};
